/*
 * @Author: 徐凯 xukai@sinoroad.com
 * @Date: 2023-11-30 11:13:41
 * @Description:
 */
import { onMounted, onActivated, onBeforeUnmount, ref } from 'vue'
import { debounce } from '@/utils'

/**
 * echarts resize
 * @returns
 */
export default function useChartResize() {
  const chart = ref(null)

  onMounted(() => {
    window.addEventListener('resize', resizeChartMethod, false)
  })

  onActivated(() => {
    // 防止 keep-alive 之后图表变形
    if (chart.value) {
      chart.value.resize()
    }
  })

  onBeforeUnmount(() => {
    window.removeEventListener('reisze', resizeChartMethod)
  })

  const resizeChartMethod = () => {
    debounce(function () {
      if (chart.value) {
        chart.value.resize()
      }
    }, 300)
  }

  return { chart }
}
